/*
* Copyright (c) 2011 Anthony Vandikas
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation 
* files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, 
* modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software 
* is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY 
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE 
* OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

#include "stdafx.h"
#include "mainwindow.h"

#include "SignOutWidget.h"
#include "HistoryWidget.h"
#include "PrintWidget.h"
#include "SettingsWidget.h"

MainWindow::MainWindow(AbstractInventory &inventory, AbstractHistory &history, AbstractStudentList &studentList, AbstractVerifier &verifier)
	: signOutWidget(new SignOutWidget(inventory, history, verifier)),
	historyWidget(new HistoryWidget(history)),
	settingsWidget(new SettingsWidget(inventory, history, studentList))
{
	ui.setupUi(this);

	// The widgets need to be added to the stack pages' layout, or they won't be seen.
	ui.signOutPage->layout()->addWidget(signOutWidget);
	ui.historyPage->layout()->addWidget(historyWidget);
	ui.settingsPage->layout()->addWidget(settingsWidget);
}

MainWindow::~MainWindow()
{
	delete signOutWidget;
	delete historyWidget;
	delete settingsWidget;
}

void MainWindow::closeEvent(QCloseEvent *event)
{
	if (!verifyPassword())
		event->ignore();
	else
		event->accept();
}

void MainWindow::signOutButtonClicked() 
{
	setPage(0);
}

void MainWindow::historyButtonClicked()
{ 	
	setPage(1); 
}

void MainWindow::viewSettings() 
{ 
	if (!verifyPassword())
		return;

	// The settings page doesn't automatically change back to the home page (the one with the three options).
	// Have to do it manually.
	settingsWidget->setPage(0);
	setPage(2);
}

void MainWindow::setPage(int index)
{	
	ui.signOutButton->setChecked(index == 0);
	ui.historyButton->setChecked(index == 1);

	// The titles are arranged in reverse order of the pages.
	// This is a throwback to when sliding functionality existed between pages.
	// This was removed for performance, but may be added back in.
	ui.titleStack->setCurrentIndex(ui.titleStack->count() - 1 - index);
	ui.pageStack->setCurrentIndex(index);
}

void MainWindow::changePassword()
{
	if (!verifyPassword())
		return;

	bool ok;
	auto newPassword = QInputDialog::getText(this, "InventoryMan", "Please enter the new password.", QLineEdit::Password, "", &ok);

	if (ok)
		settings.setValue("settings/password", newPassword);
}

void MainWindow::toggleFullscreen()
{
	if (!verifyPassword())
		return;

	if (isFullScreen())
	{
		setWindowFlags(windowFlags() ^ (Qt::CustomizeWindowHint | Qt::WindowStaysOnTopHint));
		showNormal();
	}
	else
	{
		setWindowFlags(windowFlags() | Qt::CustomizeWindowHint | Qt::WindowStaysOnTopHint);
		showFullScreen();
	}
}

bool MainWindow::verifyPassword()
{
	auto currentPassword = settings.value("settings/password", (QString)QString::null).toString();
	
	// If the password is not set or set to nothing, the dialogs become more or less an annoyance.
	if (currentPassword.isEmpty())
		return true;

	bool ok;
	auto passwordMatch = QInputDialog::getText(this, "InventoryMan", "Please verify the administrator password.", QLineEdit::Password, "", &ok);

	if (!ok)
		return false;

	if (currentPassword == passwordMatch)
		return true;

	applicationInfo("Invalid password.");
	return false;
}
